﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.Net.NetworkInformation;

namespace xuanke
{
    public partial class Start : Form
    {
        public Start()
        {
            InitializeComponent();
        }
        public bool alllicenced = false;
        public void Shut()
        {
            this.Dispose();
            this.Close();
        }
        public void error(int errorcode)
        {
            switch (errorcode)
            {
                case 1:
                    {
                        MessageBox.Show("检测到未在指定的计算机上运行此程序，请更换指定电脑。", "错误",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                        break;
                    }
                case 2:
                    {
                        MessageBox.Show("没有检测到授权信息，请联系软件作者。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        break;
                    }
                case 3:
                    {
                        MessageBox.Show("无法连接到网络", "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        break;
                    }
            }
        }
        private bool verify()
        {
            Verifyinfo.Text = "正在检查环境";
            MachineCode Code = new MachineCode();
            String cpu = Code.GetCpuInfo();
            if(1==2)
            {
                error(1);
                return false;
            }
            else return true;
        }
        private bool copyright()
        {
            Verifyinfo.Text = "正在检查网络连接";
            Ping p = new Ping();
            PingReply pr = p.Send("213.239.234.139");
            if (pr.Status == IPStatus.Success)
            {
                Verifyinfo.Text = "正在检查授权信息";
                HtmlWeb webClient = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = webClient.Load("http://liujj.orgfree.com/copyright.html");
                HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("/xuanke[1]/it");
                bool licenced = false;
                foreach (HtmlNode node in nodes)
                {
                    if (node.InnerText.Trim() == Licence.xuehao)
                        licenced = true;
                }
                if (!licenced)
                {
                    error(2);
                    return false;
                }
                else return true;
            }
            else
            {
                error(3);
                return false;
            }
        }
        private void Check_OK_Click(object sender, EventArgs e)
        {
            StateTxt.Visible=true;
            //if (verify() && copyright())
            if (copyright())
            {
                MainForm mainform = new MainForm();
                this.Hide();
                mainform.ShowDialog();
                Shut();
            }
            else
            {
                Check_OK.Enabled = false;
                Check_OK.BackColor=Color.Salmon;
                Check_OK.Text = "Stop !";
                Verifyinfo.Text = "不允许运行程序，请单击退出按钮离开。";
            }
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Shut();
        }

        private void Start_Load(object sender, EventArgs e)
        {
            this.Opacitytimecount.Enabled = true;
            this.Opacity = 0;
        }

        private void Opacitytimecount_Tick(object sender, EventArgs e)
        {
            if (this.Opacity < 0.85)
            {
                this.Opacity = this.Opacity + 0.1;
            }
            else
            {
                this.Opacitytimecount.Enabled = false;
            }
        }

    }
}
